let color = "{COOKIE_color}";
$(function () {
   $.post("./?/getBanIPList",{},function (text) {
      let data = JSON.parse(text);
      let content = ``;
      for (const key in data) {
         let v = data[key];
         let ip = key;
         let code = v.code;
         let x = `<tr><td>${ip}</td><td>${code}</td><td><a class="btn btn-sm waves-button waves-effect waves-light" onclick="editBanIP(\'${ip}\',\'${code}\')">编辑</a>&nbsp;<a class="btn btn-sm waves-button waves-effect waves-light" onclick="unbanIP(\'${ip}\')">解封</a></td></tr>`;
         content += x;
      }
      $("tbody").html(content);
      loadColor();
   });
   $("#addBanBtn").click(function () {
      $("#addBanDiv").slideDown();
   });
   $("#addBanBtnCancel").click(function () {
      $("#addBanDiv").slideUp();
      setTimeout(function () {
         $("#banIPAddress").val("");
         $("#HttpCode").val("");
      },500);
   });
   $("#addBanBtnOk").click(function () {
      let ip = $("#banIPAddress").val();
      let code = $("#HttpCode").val();
      $.post("./?/setBanIP",{
         ip:ip,
         code:code
      },function (text) {
         console.log(text);
         location.reload();
      });
   });
});

function editBanIP(IP,code) {
   $("#banIPAddress").val(IP);
   $("#HttpCode").val(code);
   $("#addBanDiv").slideDown();
}

function unbanIP(IP) {
   $.post("./?/unbanIP",{
      ip:IP
   },function (text) {
      location.reload();
   })
}